package video.background.Services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import video.background.Model.Users;
import video.background.Utils.Random;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


/**
 * @program: background
 * @description:
 * @author: hlt
 * @create: 2023-08-08 20:00
 **/
@Service
public class EmailServices {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired JavaMailSender mailSender;

    // 向用户发送验证链接用于确认邮箱
    public void sendMail(Users users, String verificationLink){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3255952007@qq.com");
        message.setTo(users.getEmail());
        message.setSubject("邮箱验证");
        message.setText("点击此链接来验证您的邮箱: " + verificationLink);
        javaMailSender.send(message);
    }

    // 向用户发送验证码用于重置密码
    public void sendMailForFindPassword(Users users, String verifyCode) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("3255952007@qq.com");
        helper.setTo(users.getEmail());
        helper.setSubject("重置密码");
        helper.setText("<html><body><div class = \"body\"><span>Hello,  </span><a>"+ users.getEmail() +"<a><br><p>您正在重置密码，您的验证码为：</p><h2>" + verifyCode +"</h2><p>验证码5分钟内有效。</p> <p>请务必妥善保护您的验证码，若非您本人操作，请忽略本条消息。</p></div><style>.body{width:70%;height:70%;border:20px solid #32be8f;border-radius:5px;padding:10%;background-color:#f1f6f4}</style></body></html>", true);
        javaMailSender.send(message);
    }

    // 注册前先验证用户的邮箱是否有效
    public void sendMailForVerify(Users users, String verifyCode) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("3255952007@qq.com");
        helper.setTo(users.getEmail());
        helper.setSubject("注册账号");
        helper.setText("<html><body><div class = \"body\"><span>Hello,  </span><a>"+ users.getEmail() +"<a><br><p>您正在注册账号，您的验证码为：</p><h2>" + verifyCode +"</h2><p>验证码5分钟内有效。</p> <p>请务必妥善保护您的验证码，若非您本人操作，请忽略本条消息。</p></div><style>.body{width:70%;height:70%;border:20px solid #32be8f;border-radius:5px;padding:10%;background-color:#f1f6f4}</style></body></html>", true);
        javaMailSender.send(message);
    }

    // 注销账户前验证用户邮箱
    public void sendMessageForGiveUp(Users users, String verifyCode) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("3255952007@qq.com");
        helper.setTo(users.getEmail());
        helper.setSubject("注销账号");
        helper.setText("<html><body><div class = \"body\"><span>Hello,  </span><a>"+ users.getEmail() +"<a><br><p>您正在注销账号，您的验证码为：</p><h2>" + verifyCode +"</h2><p>验证码5分钟内有效。</p> <p>请务必妥善保护您的验证码，若非您本人操作，请忽略本条消息。</p></div><style>.body{width:70%;height:70%;border:20px solid #32be8f;border-radius:5px;padding:10%;background-color:#f1f6f4}</style></body></html>", true);
        javaMailSender.send(message);
    }
}
